package cn.edu.scau.cmi.chenjiale.last;

import cn.edu.scau.cmi.chenjiale.adapter.AdapterApplication;
import cn.edu.scau.cmi.chenjiale.composite.CompositeClient;
import cn.edu.scau.cmi.chenjiale.factory.first.FirstInsuranceApplication;
import cn.edu.scau.cmi.chenjiale.factory.second.SecondInsuranceApplication;
import cn.edu.scau.cmi.chenjiale.singleton.ClientApplication;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

/**
 * 主启动类
 * 
 * @author chen.jiale
 * @date 2019/05/26
 *
 */
public class LastApplication extends Application {

	@Override
	public void start(Stage primaryStage) throws Exception {
		BorderPane totalPane = new BorderPane();
		BorderPane imagePane = this.getImagePane();
		GridPane btnPane = this.getButtonPane();
		totalPane.setLeft(imagePane);
		totalPane.setCenter(btnPane);
		primaryStage.setTitle("综合性实验");
		primaryStage.setScene(new Scene(totalPane, 400, 300));
		primaryStage.show();

	}

	/**
	 * 获取按钮面板
	 * 
	 * @return
	 */
	private GridPane getButtonPane() {
		GridPane btnPane = new GridPane();
		btnPane.setHgap(10);
		btnPane.setVgap(20);
		Button singleBtn = new Button("实验一：单例模式&多例模式");
		Button factoryBtn1 = new Button("实验二：简单工厂模式");
		Button factoryBtn2 = new Button("实验二：工厂方法模式");
		Button factoryBtn3 = new Button("实验二：抽象工厂模式");
		Button adapterBtn = new Button("实验三：适配器模式");
		Button compositeBtn = new Button("实验四：组合模式");

		btnPane.setAlignment(Pos.TOP_CENTER);
		btnPane.addColumn(0, singleBtn, factoryBtn1, factoryBtn2, factoryBtn3, adapterBtn, compositeBtn);

		singleton(singleBtn);
		factory(factoryBtn1, factoryBtn2, factoryBtn3);
		adapter(adapterBtn);
		composite(compositeBtn);
		return btnPane;
	}

	/**
	 * 实验一，单例模式与多例模式
	 * 
	 * @param singleBtn
	 */
	private void singleton(Button singleBtn) {
		singleBtn.setOnAction(e -> {
			Platform.runLater(new Runnable() {
				@Override
				public void run() {
					ClientApplication application = new ClientApplication();
					try {
						application.start(new Stage());
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			});

		});
	}

	/**
	 * 实验二：工厂模式
	 * 
	 * @param btn1 简单工厂模式
	 * @param btn2 工厂方法模式
	 * @param btn3 抽象工厂模式
	 */
	private void factory(Button btn1, Button btn2, Button btn3) {
		// 简单工厂模式
		btn1.setOnAction(e -> {
			Platform.runLater(new Runnable() {

				@Override
				public void run() {
					FirstInsuranceApplication application = new FirstInsuranceApplication();
					try {
						application.start(new Stage());
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			});
		});

		// 工厂方法模式
		btn2.setOnAction(e -> {
			Platform.runLater(new Runnable() {

				@Override
				public void run() {
					SecondInsuranceApplication application = new SecondInsuranceApplication();
					try {
						application.start(new Stage());
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			});
		});

		// 抽象工厂模式
		btn3.setOnAction(e -> {
			Platform.runLater(new Runnable() {

				@Override
				public void run() {

				}
			});
		});
	}

	/**
	 * 实验三：适配器模式
	 * 
	 * @param adapterBtn
	 */
	private void adapter(Button adapterBtn) {
		// 对象
		adapterBtn.setOnAction(e -> {
			Platform.runLater(new Runnable() {

				@Override
				public void run() {
					AdapterApplication application = new AdapterApplication();
					try {
						application.start(new Stage());
					} catch (Exception e) {
						e.printStackTrace();
					}

				}
			});
		});
	}

	/**
	 * 实验四：组合模式
	 * 
	 * @param compositeBtn
	 */
	private void composite(Button compositeBtn) {
		compositeBtn.setOnAction(e -> {
			Platform.runLater(new Runnable() {

				@Override
				public void run() {
					CompositeClient client = new CompositeClient();
					try {
						client.start(new Stage());
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			});
		});

	}

	/**
	 * 获取左侧面板
	 * 
	 * @return
	 */
	private BorderPane getImagePane() {
		BorderPane pane = new BorderPane();
		// 照片
		Image image = new Image("image/1.jpg");
		ImageView imageView = new ImageView(image);
		imageView.setFitHeight(200);
		imageView.setFitWidth(200);
		// 个人信息
		GridPane infoPane = new GridPane();
		infoPane.setHgap(10);
		infoPane.setVgap(10);
		infoPane.add(new Label("   学号:"), 0, 0);
		infoPane.add(new Label("201627010604"), 1, 0);
		infoPane.add(new Label("   姓名:"), 0, 1);
		infoPane.add(new Label("陈佳乐"), 1, 1);

		pane.setTop(imageView);
		pane.setCenter(infoPane);
		return pane;
	}

	public static void main(String[] args) {
		Application.launch(args);

	}

}
